package vue;

import controleur.EcouteurDeSourisJeu;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import modele.Joueur;
import modele.Plateau;
import modele.epoque.ex.EpoqueInexistante;
import modele.mode.Mode;
import outils.Outils;
import outils.gestionnairefichier.GestionnairePlateau;

/**
 *
 * @author twane
 */
public class Fenetre extends javax.swing.JFrame {

	/**
	 * Creates new form Fenetre
	 */
	private AireDeJeu aire;
	private Plateau plateau;
	private Joueur j1;
	private Joueur j2;
	private PanneauLateral panneauLateral;

	/**
	 * utilisé par netbeans
	 */
	public Fenetre() {
	}

	/**
	 * Crée une nouvelle Fenetre avec un nouveau plateau et deux nouveau joueurs
	 *
	 * @param cheminPlateau l'adresse du fichier contenant le plateau
	 * @param epoqueJ1 l'epoque du joueur 1
	 * @param epoqueJ2 l'epoque du joueur 2
	 * @param placementAuto vrai si l'on doit placer automatiquement les pions,
	 * faux sinon
	 * @param mode le mode de jeu
	 */
	public Fenetre(String cheminPlateau, String epoqueJ1, String epoqueJ2, final boolean placementAuto, Mode mode) {
		try {
			setUndecorated(true);
			setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
			initComponents();
			plateau = GestionnairePlateau.getInstance().get("src/donnees/maps/" + cheminPlateau).copier();
			plateau.setMode(mode);

			j1 = new Joueur(1, epoqueJ1);
			j1.setNom("Joueur 1");

			j2 = new Joueur(2, epoqueJ2);
			j2.setNom("Joueur 2");

			final Fenetre f = this;
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					aire = new AireDeJeu(fondAire, plateau, j1, j2, f);

					fondAire.add(aire);

					panneauLateral = new PanneauLateralEdition(j1, j2, aire);

					ajouterDansPanel(panneauLateralDesigner, panneauLateral);


					miniMap.creerMiniMap(aire);
					miniMap.actualiserMiniMap();

					if (placementAuto) {
						passerEnPartie();
						plateau.placementAutomatiquePions(j1);
						plateau.placementAutomatiquePions(j2);
						aire.effacerZoneDeplacement();
						aire.actualiserBuffers();
						actualiserMiniMap();
						panneauLateral.actualiserPanelJoueur();
					}
				}
			});

		} catch (EpoqueInexistante ex) {
			Outils.erreur("L'epoque n'existe pas.");
		}
	}

	/**
	 * Signigie que l'on a fini l'édition du plateau et que l'on passe en mode
	 * partie
	 */
	public void passerEnPartie() {
		aire.setEcouteur(new EcouteurDeSourisJeu(aire, plateau, j1, j2, this));
		panneauLateral = new PanneauLateralPartie(j1, j2, aire);
		panneauLateralDesigner.removeAll();
		ajouterDansPanel(panneauLateralDesigner, panneauLateral);
	}

	/**
	 * Change le panel d'info affiché, passe à celui de l'autre joueur
	 */
	public void inverserPanelJoueur() {
		panneauLateral.inverserPanelJoueur();

	}

	/**
	 * Actualise le panel d'info du joueur Courant
	 */
	public void actualiserPanelJoueur() {
		panneauLateral.actualiserPanelJoueur();
	}

	/**
	 * Actualise la miniMap
	 */
	public void actualiserMiniMap() {
		miniMap.actualiserMiniMap();
	}

	/**
	 *
	 * @return le PanneauLateral
	 */
	public PanneauLateral getPanneauLateral() {
		return panneauLateral;
	}

	/**
	 * Fonction permettant d'ajouter un JPanel 'contenu' dans un autre JPanel
	 * 'conteneur'
	 *
	 * @param conteneur le conteneur
	 * @param p le contenu
	 */
	public static void ajouterDansPanel(JPanel conteneur, JPanel p) {
		javax.swing.GroupLayout PanneauLateralLayout = new javax.swing.GroupLayout(conteneur);
		conteneur.setLayout(PanneauLateralLayout);
		PanneauLateralLayout.setHorizontalGroup(
				PanneauLateralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(PanneauLateralLayout.createSequentialGroup().addComponent(p, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(0, 0, Short.MAX_VALUE)));
		PanneauLateralLayout.setVerticalGroup(
				PanneauLateralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(PanneauLateralLayout.createSequentialGroup().addComponent(p, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(0, 0, Short.MAX_VALUE)));
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        FondFullScreen = new javax.swing.JPanel();
        BoutonQuitter = new javax.swing.JButton();
        fondAire = new javax.swing.JPanel();
        panneauLateralDesigner = new javax.swing.JPanel();
        miniMap = new vue.MiniMap();
        coupSpecialButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        FondFullScreen.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());

        BoutonQuitter.setIcon(new javax.swing.ImageIcon(getClass().getResource("/donnees/images/Croix.png"))); // NOI18N
        BoutonQuitter.setBorder(null);
        BoutonQuitter.setBorderPainted(false);
        BoutonQuitter.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        BoutonQuitter.setFocusable(false);
        BoutonQuitter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BoutonQuitterActionPerformed(evt);
            }
        });

        fondAire.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        fondAire.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize().width*3/4,100));

        javax.swing.GroupLayout fondAireLayout = new javax.swing.GroupLayout(fondAire);
        fondAire.setLayout(fondAireLayout);
        fondAireLayout.setHorizontalGroup(
            fondAireLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 254, Short.MAX_VALUE)
        );
        fondAireLayout.setVerticalGroup(
            fondAireLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 452, Short.MAX_VALUE)
        );

        panneauLateralDesigner.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        panneauLateralDesigner.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize().width/4,100));

        javax.swing.GroupLayout panneauLateralDesignerLayout = new javax.swing.GroupLayout(panneauLateralDesigner);
        panneauLateralDesigner.setLayout(panneauLateralDesignerLayout);
        panneauLateralDesignerLayout.setHorizontalGroup(
            panneauLateralDesignerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 301, Short.MAX_VALUE)
        );
        panneauLateralDesignerLayout.setVerticalGroup(
            panneauLateralDesignerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 188, Short.MAX_VALUE)
        );

        miniMap.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        miniMap.setPreferredSize(new java.awt.Dimension(200, 200));

        javax.swing.GroupLayout miniMapLayout = new javax.swing.GroupLayout(miniMap);
        miniMap.setLayout(miniMapLayout);
        miniMapLayout.setHorizontalGroup(
            miniMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 198, Short.MAX_VALUE)
        );
        miniMapLayout.setVerticalGroup(
            miniMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 198, Short.MAX_VALUE)
        );

        coupSpecialButton.setForeground(new java.awt.Color(255, 255, 255));
        coupSpecialButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/donnees/images/Transparent.png"))); // NOI18N
        coupSpecialButton.setText(" ");
        coupSpecialButton.setBorder(null);
        coupSpecialButton.setBorderPainted(false);
        coupSpecialButton.setFocusPainted(false);
        coupSpecialButton.setFocusable(false);
        coupSpecialButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        coupSpecialButton.setIconTextGap(0);
        coupSpecialButton.setMaximumSize(new java.awt.Dimension(50, 50));
        coupSpecialButton.setMinimumSize(new java.awt.Dimension(50, 50));
        coupSpecialButton.setPreferredSize(new java.awt.Dimension(50, 50));
        coupSpecialButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                coupSpecialButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout FondFullScreenLayout = new javax.swing.GroupLayout(FondFullScreen);
        FondFullScreen.setLayout(FondFullScreenLayout);
        FondFullScreenLayout.setHorizontalGroup(
            FondFullScreenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, FondFullScreenLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(fondAire, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                .addGroup(FondFullScreenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(FondFullScreenLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(FondFullScreenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(FondFullScreenLayout.createSequentialGroup()
                                .addComponent(coupSpecialButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BoutonQuitter))
                            .addComponent(panneauLateralDesigner, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(FondFullScreenLayout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(miniMap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        FondFullScreenLayout.setVerticalGroup(
            FondFullScreenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, FondFullScreenLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(FondFullScreenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(fondAire, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, FondFullScreenLayout.createSequentialGroup()
                        .addGroup(FondFullScreenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(BoutonQuitter)
                            .addComponent(coupSpecialButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panneauLateralDesigner, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(miniMap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(FondFullScreen, javax.swing.GroupLayout.DEFAULT_SIZE, 591, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(FondFullScreen, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BoutonQuitterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BoutonQuitterActionPerformed
		dispose();
		System.exit(0);
    }//GEN-LAST:event_BoutonQuitterActionPerformed

    private void coupSpecialButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_coupSpecialButtonActionPerformed
		outils.animations.AnimationSpeciale.animationCroix(aire, BoutonQuitter);
    }//GEN-LAST:event_coupSpecialButtonActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(Fenetre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(Fenetre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(Fenetre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(Fenetre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				new Fenetre().setVisible(true);
			}
		});
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BoutonQuitter;
    private javax.swing.JPanel FondFullScreen;
    private javax.swing.JButton coupSpecialButton;
    private javax.swing.JPanel fondAire;
    private vue.MiniMap miniMap;
    private javax.swing.JPanel panneauLateralDesigner;
    // End of variables declaration//GEN-END:variables
}
